import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';

import 'sunmi_status_light_manager_platform_interface.dart';
import 'sunmi_status_light_manager.dart';

/// An implementation of [SunmiStatusLightManagerPlatform] that uses method channels.
class MethodChannelSunmiStatusLightManager
    extends SunmiStatusLightManagerPlatform {
  /// The method channel used to interact with the native platform.
  @visibleForTesting
  final methodChannel = const MethodChannel('sunmi_status_light_manager');

  @override
  Future<bool> initialize() async {
    try {
      final result = await methodChannel.invokeMethod<bool>('initialize');
      return result ?? false;
    } catch (e) {
      debugPrint(
          'SunmiStatusLightManagerPlatform--->Error initializing status light manager: $e');
      return false;
    }
  }

  @override
  Future<bool> setColor(StatusLightColor color) async {
    try {
      final result = await methodChannel.invokeMethod<bool>('setColor', {
        'color': color.name,
      });
      return result ?? false;
    } catch (e) {
      debugPrint('SunmiStatusLightManagerPlatform--->Error setting color: $e');
      return false;
    }
  }

  @override
  Future<bool> turnOff() async {
    try {
      final result = await methodChannel.invokeMethod<bool>('turnOff');
      return result ?? false;
    } catch (e) {
      debugPrint(
          'SunmiStatusLightManagerPlatform--->Error turning off status light: $e');
      return false;
    }
  }

  @override
  Future<bool> setFlashing(StatusLightColor color, int onMs, int offMs) async {
    try {
      final result = await methodChannel.invokeMethod<bool>('setFlashing', {
        'color': color.name,
        'onMs': onMs,
        'offMs': offMs,
      });
      return result ?? false;
    } catch (e) {
      debugPrint(
          'SunmiStatusLightManagerPlatform--->Error setting flashing: $e');
      return false;
    }
  }

  @override
  Future<bool> setMultiFlashing(
    List<StatusLightColor> colors,
    List<int> onMs,
    List<int> offMs,
  ) async {
    try {
      final result =
          await methodChannel.invokeMethod<bool>('setMultiFlashing', {
        'colors': colors.map((c) => c.name).toList(),
        'onMs': onMs,
        'offMs': offMs,
      });
      return result ?? false;
    } catch (e) {
      debugPrint(
          'SunmiStatusLightManagerPlatform--->Error setting multi flashing: $e');
      return false;
    }
  }

  @override
  Future<bool> destroy() async {
    try {
      final result = await methodChannel.invokeMethod<bool>('destroy');
      return result ?? false;
    } catch (e) {
      debugPrint(
          'SunmiStatusLightManagerPlatform--->Error destroying status light manager: $e');
      return false;
    }
  }
}
